<?php
/**
 * Created by PhpStorm.
 * User: 17628
 * Date: 2021-10-13
 * Time: 10:44
 */

namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\GetBuy as GetBuyModel;
use app\admin\model\CommoditySkuValue as CommoditySkuValueModel;
use think\Env;
class Groupbuy extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 拼购显示
     *
     */
    public function index()
    {
        $getbuy = GetBuyModel::where('out_time', '<', time())
            ->field('id,image,price,title,group,express,serve,content,value_id')
            ->select();
        if (!empty($getbuy)) {
            foreach ($getbuy as &$v) {
                $value = CommoditySkuValueModel::where('id', 'in', $v['value_id'])
                    ->with('sku')
                    ->field('id,name,sku_id')
                    ->select();
                $sku = [];
                foreach ($value as &$vv) {
                    $sku[$vv['sku_id']]['name'] = $vv['sku']['name'];
                    unset($vv['sku']);
                    $sku[$vv['sku_id']]['values'][] = $vv;
                }
                $v['value_id'] = $sku;
                $image = explode(',', $v['image']);
                $domain = [];
                foreach ($image as $vvv) {
                    $domain[] = Env::get('database.domin') . $vvv;
                }
                $v['image'] = $domain;
                $v['content'] = get_img_thumb_url($v['content']);
                $v['out_time'] = date('Y-m-d H:i:s', $v['out_time']);
                $v->unsetAppend();
                unset($v);
            }
        }
        $this->success('获取成功', $getbuy);
    }

}